import { Container, injectable, interfaces } from "inversify";
import { IServiceContainer } from "./types";

@injectable()
export class ServiceContainer implements IServiceContainer {
    constructor(private container: Container) { }
    get<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, name?: string | number | symbol): T {
        return name ? this.container.getNamed<T>(serviceIdentifier, name) : this.container.get<T>(serviceIdentifier);
    }
    getAll<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, name?: string | number | symbol): T[] {
        return name
            ? this.container.getAllNamed<T>(serviceIdentifier, name)
            : this.container.getAll<T>(serviceIdentifier);
    }
    tryGet<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, name?: string | number | symbol): T | undefined {
        try {
            return name
                ? this.container.getNamed<T>(serviceIdentifier, name)
                : this.container.get<T>(serviceIdentifier);
        } catch {
            // This might happen after the container has been destroyed
        }

        return undefined;
    }
}